// pages/visitor/visitor.js
let util = require("../../utils/util");
let md5 = require("../../utils/md5.js");
const app = getApp()
// QQMapKey是在腾讯地图开放平台设置的密钥
// 请使用你自己的密钥
const QQMapKey = '5J2BZ-2NORQ-5FE5E-GMJV6-EWMLO-K6FPJ';

// 在密钥设置中开启WebServiceAPI，选择签名校验，即可获取Secret key，即SK
// 请使用你自己的SK
const SK = '2lV4WrSKcjApGrYGBROiEIlUGD0xaFas';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    uid: '',
    name: '',
    temperature: '',
    showUploadBtn: true,
    message: '',
    address: null,
    lati: '',
    long: '',
    school: '',
    class: '',
    phone: ''
  },

  inputTemperature: function (e) {
    this.setData({
      temperature: e.detail.value
    })
    this.temperature = e.detail.value
  },


  // 根据经纬度，逆地址解析
  getAddress() {
    var that = this
    let SIG = md5("/ws/geocoder/v1?key=" + QQMapKey + "&location="
      + String(app.globalData.lati) + "," + String(app.globalData.long) + SK)
    let info = {
      key: QQMapKey,
      location: `${app.globalData.lati},${app.globalData.long}`,
      sig: SIG
    }
    wx.cloud.callFunction({
      name: 'doPost',
      data: {
        url: 'https://apis.map.qq.com/ws/geocoder/v1',
        info: info
      },
    }).then(res => {
      let result = res.result.result
      // formatted_addresses.recommend是经过腾讯地图优化过的描述方式，更具人性化特点
      let formatted_addresses = result.address + result.formatted_addresses.recommend
      that.address = formatted_addresses
      that.setData({
        address: formatted_addresses
      })
    })
  },


  //上传
  upLoadRecord: function (e) {
    const that = this
    that.lati = app.globalData.lati
    that.long = app.globalData.long
    let info =
    {
      uid: that.uid,
      date: that.date,
      temperature: that.temperature,
      latitude: that.lati,
      longitude: that.long,
      address: that.address
    }
    wx.cloud.callFunction({
      name: 'doPost',
      data: {
        url: app.globalData.url + '/user/uploadRecord',
        info: info,
        token: app.globalData.token
      },
    }).then(res => {
      that.setData({
        showUploadBtn: false,
        message: '打卡成功!'
      })
    })


  },

  getLocation: function (e) {
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        app.globalData.lati = res.latitude
        app.globalData.long = res.longitude
      },
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const that = this
    that.getLocation()
    that.date = util.formatTime(new Date()).split(" ")[0]
    that.uid = app.globalData.userId
    that.getAddress()
    wx.cloud.callFunction({
      name: 'doPost',
      data: {
        url: app.globalData.url + '/user/hasRecord?uid=' + that.uid + '&date=' + that.date,
        info: "",
        token: app.globalData.token
      },
    }).then(res => {
      if (res.result == true) {
        that.setData({
          showUploadBtn: false,
          message: '你已打卡!'
        })
      }
      else {
        that.setData({
          showUploadBtn: true,
        })
      }
    }).catch(res => {
      console.log(res)
    })
    that.setData({
      date: that.date,
      name: app.globalData.name,
      uid: app.globalData.userId,
      school: app.globalData.school,
      class: app.globalData.class,
      phone: app.globalData.phone
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})